package lfr.ustc.ftp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class Setting extends Activity {

	protected EditText pwd_etd = null;
	protected EditText user_edt = null;
	protected Button cancel = null;
	protected Button back = null;
	protected CheckBox needPwd_ckb = null;

	private SharedPreferences settings; // ���ڴ洢�û�������û����������Ϣ
	SharedPreferences.Editor editor;

	/* �Զ����û�������� */
	private String userName;
	private String pwd;

	private boolean needPwd = false; // �Ƿ���Ҫ�������

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		init();
		needPwd_ckb
				.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()

				{

					/* implement onCheckedChanged���� */

					@Override
					public void onCheckedChanged(CompoundButton buttonView,

					boolean isChecked)

					{
						if (isChecked) {
							user_edt.setEnabled(true);
							pwd_etd.setEnabled(true);
						} else {
							user_edt.setEnabled(false);
							pwd_etd.setEnabled(false);
						}
					}
				});

		back.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				userName = user_edt.getText().toString();
				pwd = pwd_etd.getText().toString();
				needPwd = needPwd_ckb.isChecked();
				if (needPwd) {
                     if(userName.trim().equals("")){
                    	 ToastShow("用户名不能为空");
 						return;
                     }else if(!isValidate(userName)){
                    	 ToastShow("用户名只能是数字和英文");
                    	 return;
                     }else if(isValidate(userName)){
                    	 editor.putString("username", userName);
                     }
                     if(pwd.trim().equals("")){
                    	 ToastShow("密码不能为空");
 						return;
                     }else if(!isValidate(pwd)){
                    	 ToastShow("密码只能是数字和英文");
                    	 return;
                     }else if(isValidate(pwd)){
                    	 editor.putString("pwd", pwd);
                     }
					

				}
				editor.putBoolean("needpwd", needPwd);
				editor.commit();
				exit();
			}

		});
		cancel.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				exit();
			}

		});
	}
	private void ToastShow(String str){
		Toast.makeText(this, str, 2000).show();
	}

	private void init() {
		this.back = (Button) this.findViewById(R.id.btn_ok);
		this.cancel = (Button) this.findViewById(R.id.btn_cancel);
		this.user_edt = (EditText) this.findViewById(R.id.user);
		this.pwd_etd = (EditText) this.findViewById(R.id.pwd);
		this.needPwd_ckb = (CheckBox) this.findViewById(R.id.chk);
		settings = CommonSettings.getSettings();
		;
		editor = settings.edit();
		this.pwd = settings.getString("pwd", "");
		this.userName = settings.getString("username", "");
		this.needPwd = settings.getBoolean("needpwd", false);
		this.pwd_etd.setText(pwd);
		this.user_edt.setText(userName);
		if (needPwd) {
			this.needPwd_ckb.setChecked(true);
			this.user_edt.setEnabled(true);
			this.pwd_etd.setEnabled(true);

		} else {
			this.user_edt.setEnabled(false);
			this.pwd_etd.setEnabled(false);
		}

	}


   
	
	public static boolean isValidate(String str) {
		boolean isRightString = true;
		final String stringFormat = "^[A-Za-z0-9]+$";
		final Pattern pattern = Pattern.compile(stringFormat);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			isRightString = false;
		}
		return isRightString;
	}
	private void exit() {
		this.finish();
	}

}